﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using FlexiCommerce.Components;

namespace FlexiCommerce.Web.Handlers
{
    public class TranslationCarrier : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            int itemID;
            CultureInfo requestedCulture = null;
            PersistenceItem item;

            //TODO: Fix status codes
            if (string.IsNullOrEmpty("language"))
                return;

            try
            {
                requestedCulture = new CultureInfo(context.Request["language"]);
            }
            catch (Exception)
            {
                return;
            }

            if (requestedCulture == null)
                return;

            if (string.IsNullOrEmpty(context.Request["categoryid"]))
            {
                return;
            }
            else
            {
                itemID = int.Parse(context.Request["categoryid"]);
                item = Find.Categories.Single(c => c.ID == itemID);
            }

            if (item == null)
            {
                return;
            }

            Type type = item.GetType();

            if (type == typeof(Category))
            {
                context.Response.Redirect(FlexiCommerce.Context.UrlParser.BuildUrl(item as Category, requestedCulture), true);
            }

        }

        #endregion
    }
}
